package com.flick.searchShop.service;

import com.flick.po.ShopInfo;
import com.flick.po.ShopSales;
import com.flick.po.ShopType;
import com.flick.searchShop.dao.ShopInfoDao;
import com.flick.searchShop.dao.ShopSalesDao;
import com.flick.searchShop.dao.ShopTypeDao;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
import org.springframework.web.client.AsyncRestTemplate;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author flick on 2017/4/26.
 * @version 1.0
 */
@Service
public class ProduceServ {
    @Autowired
    ShopTypeDao shopTypeDao;
    @Autowired
    ShopInfoDao shopInfoDao;
    @Autowired
    ShopSalesDao shopSalesDao;
    @Autowired
    RestTemplate restTemplate;

    public void getProduce() throws Exception{
        List<ShopType> list = shopTypeDao.list();
        for (ShopType shopType : list) {
            String oprtCatNo = shopType.getOprtCatNo();
            if (oprtCatNo.length() <= 3) {
                int total = shopType.getTotal();
                int pageSize = shopType.getPageSize();
                int maxPage = (int) Math.ceil((double) total / pageSize);
                for (int i = 1; i <= maxPage; i++) {
                    int pageNumber = i;
                    System.out.println(oprtCatNo + ":name=" + shopType.getTypeName() + ":maxPage=" + pageNumber);
                    Map<String, Object> urlVariables = new HashMap<String, Object>();
                    urlVariables.put("pageNo", pageNumber);
                    urlVariables.put("oprtCatNo", oprtCatNo);
                    String url = "http://search.ccoop.com.cn/mall/CommodityList/searchCommoByOrder.ihtml?pageNo=" + pageNumber + "&oprtCatNo" + oprtCatNo;
                    //                                String url = "http://search.ccoop.com.cn/mall/CommodityList/searchCommoByOrder.ihtml";
                    String prodUrl = "http://item.ccoop.com.cn/mall/commodity/getCommodityDetail.ihtml?prodNo=";
                    AsyncRestTemplate restTemplate = new AsyncRestTemplate();
                    ListenableFuture<ResponseEntity<String>> shopListen = restTemplate.getForEntity(url, String.class);
                    shopListen.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
                        @Override
                        public void onSuccess(ResponseEntity<String> result) {
                            try {
                                String body = result.getBody();
                                ObjectMapper mapper = new ObjectMapper();
                                JsonNode node = mapper.readTree(body);
                                JsonNode docs = node.get("docs");
                                for (JsonNode doc : docs) {
                                    ShopInfo shopInfo = new ShopInfo();
                                    shopInfo.setCommoNo(doc.get("commoNo").asText());
                                    String defProdNo = doc.get("defProdNo").asText();
                                    shopInfo.setDefProdNo(defProdNo);
                                    shopInfo.setProdUrl(prodUrl + doc.get("defProdNo").asText());
                                    shopInfo.setShopType(shopType);
                                    shopInfo.setBrand(doc.get("brand").asText());
                                    shopInfo.setTitle(doc.get("title").asText());
                                    shopInfo.setListTime(doc.get("listTime").asText());
                                    shopInfo.setShopNo(doc.get("shopNo").asText());
                                    shopInfo.setPrice(doc.get("price").asText());
                                    shopInfo.setMarkprice(doc.get("markprice").asText());
                                    //                                    shopInfoDao.insert(shopInfo);
                                    String salesUrl = "http://item.ccoop.com.cn/mall/commodity/getCommodityDetailInfo.ihtml?prodNo=" + defProdNo;
                                    AsyncRestTemplate restTemplate2 = new AsyncRestTemplate();
                                    ListenableFuture<ResponseEntity<String>> salesListen = restTemplate2.getForEntity(salesUrl, String.class);
                                    salesListen.addCallback(new ListenableFutureCallback<ResponseEntity<String>>() {
                                        @Override
                                        public void onSuccess(ResponseEntity<String> stringResponseEntity) {
                                            try {
                                                String salesBody = stringResponseEntity.getBody();
                                                ObjectMapper salesMapper = new ObjectMapper();
                                                JsonNode salesNode = salesMapper.readTree(salesBody);
                                                ShopSales shopSales = new ShopSales();
                                                shopSales.setProductStock(salesNode.get("productStock").asText());
                                                shopSales.setSalesNum(salesNode.get("salesNum").asText());
                                                shopSales.setShopInfo(shopInfo);
                                                shopSalesDao.insert(shopSales);
                                                //                                                        Thread.sleep(200);
                                                System.out.println(shopInfo.getTitle() + "," + shopSales.getSalesNum());
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }

                                        @Override
                                        public void onFailure(Throwable throwable) {
                                            System.out.println("======client2 failure : " + throwable);
                                        }
                                    });

                                }
                                System.out.println("======client1 get result : " + result.getBody());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            System.out.println("======client failure : " + t);
                        }

                    });
                }
            }
        }
    }
    public void getProduceSingle() throws Exception{
        List<ShopType> list = shopTypeDao.list();
        for (ShopType shopType : list) {
            String oprtCatNo = shopType.getOprtCatNo();
            if (oprtCatNo.length() <= 3) {
                int total = shopType.getTotal();
                int pageSize = shopType.getPageSize();
                int maxPage = (int) Math.ceil((double) total / pageSize);
                for (int i = 1; i <= maxPage; i++) {
                    int pageNumber = i;
                    System.out.println(oprtCatNo + ":name=" + shopType.getTypeName() + ":maxPage=" + pageNumber);
                    Map<String, Object> urlVariables = new HashMap<String, Object>();
                    urlVariables.put("pageNo", pageNumber);
                    urlVariables.put("oprtCatNo", oprtCatNo);
                    String url = "http://search.ccoop.com.cn/mall/CommodityList/searchCommoByOrder.ihtml?pageNo=" + pageNumber + "&oprtCatNo" + oprtCatNo;
                    //                                String url = "http://search.ccoop.com.cn/mall/CommodityList/searchCommoByOrder.ihtml";
                    String prodUrl = "http://item.ccoop.com.cn/mall/commodity/getCommodityDetail.ihtml?prodNo=";
                    String shopRes = restTemplate.getForObject(url, String.class);
                    ObjectMapper mapper = new ObjectMapper();
                    JsonNode node = mapper.readTree(shopRes);
                    JsonNode docs = node.get("docs");
                    for (JsonNode doc : docs) {
                        ShopInfo shopInfo = new ShopInfo();
                        shopInfo.setCommoNo(doc.get("commoNo").asText());
                        String defProdNo = doc.get("defProdNo").asText();
                        shopInfo.setDefProdNo(defProdNo);
                        shopInfo.setProdUrl(prodUrl + doc.get("defProdNo").asText());
                        shopInfo.setShopType(shopType);
                        shopInfo.setBrand(doc.get("brand").asText());
                        shopInfo.setTitle(doc.get("title").asText());
                        shopInfo.setListTime(doc.get("listTime").asText());
                        shopInfo.setShopNo(doc.get("shopNo").asText());
                        shopInfo.setPrice(doc.get("price").asText());
                        shopInfo.setMarkprice(doc.get("markprice").asText());
//                                    shopInfoDao.insert(shopInfo);
                        String salesUrl = "http://item.ccoop.com.cn/mall/commodity/getCommodityDetailInfo.ihtml?prodNo=" + defProdNo;
                        String salesRes = restTemplate.getForObject(salesUrl, String.class);
                        JsonNode salesNode = mapper.readTree(salesRes);
                        ShopSales shopSales = new ShopSales();
                        shopSales.setProductStock(salesNode.get("productStock").asText());
                        shopSales.setSalesNum(salesNode.get("salesNum").asText());
                        shopSales.setShopInfo(shopInfo);
                        shopSalesDao.insert(shopSales);
                        System.out.println(shopInfo.getTitle() + "," + shopSales.getSalesNum());
                    }
                }
            }
        }
    }
}
